/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Contrast.java
 *
 * Created on 14/02/2009, 18:30:52
 */

package edu.upf.grupE.interf;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JSlider;

/**
 *
 * @author Oscar
 */
public class Blur extends javax.swing.JFrame {

    int param1 = 150;
  Icon imageIcon = new ImageIcon(getClass().getResource("/img/ima_new_paisaje_001.jpg"));

    private ImageIcon scale(Image src, double scale) {
        int w = (int)(scale*src.getWidth(this));
        int h = (int)(scale*src.getHeight(this));
        int type = BufferedImage.TYPE_INT_RGB;
        BufferedImage dst = new BufferedImage(w, h, type);
        Graphics2D g2 = dst.createGraphics();
        g2.drawImage(src, 0, 0, w, h, this);
        g2.dispose();
        return new ImageIcon(dst);
    }

    /** Creates new form Contrast */
    public Blur() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSlider2 = new javax.swing.JSlider();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Blur");

        jSlider2.setPaintLabels(true);
        jSlider2.setPaintTicks(true);
        jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider2StateChanged(evt);
            }
        });

        jXLabel2.setText("Radius");

        jButton1.setText("OK");

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextField2.setEnabled(false);

        ImageIcon icon= new ImageIcon(getClass().getResource("/img/ima_new_paisaje_001.jpg"));
        ImageIcon reduced= scale(icon.getImage(), 0.40);
        jLabel1.setIcon(reduced);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(93, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(81, 81, 81))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addGap(86, 86, 86)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSlider2, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                            .addComponent(jXLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton1))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

public void repaint() {
        // Beware of a race condition when Swing tries to repaint
        // before we've finished initialization.
        if(imageIcon instanceof IconJAI) {
            RenderedImage previousImage  = ((IconJAI)imageIcon).getImage();
            if(previousImage instanceof PlanarImage) {
                ((PlanarImage)previousImage).dispose();
            }
        }
           imageIcon = new IconJAI(process());

        jLabel1.setIcon(imageIcon);
        Dimension iconSize = new Dimension(imageIcon.getIconWidth(),
                                           imageIcon.getIconHeight());
        jLabel1.setPreferredSize(iconSize);
        jLabel1.revalidate();

  
    }

     public PlanarImage process() {
 
        PlanarImage im = (PlanarImage)JAI.create("fileload", "C:\\nerd.jpg");

        ParameterBlock pb = new ParameterBlock();
        Interpolation interp = Interpolation.getInstance(
                                        Interpolation.INTERP_BILINEAR);

        ParameterBlock params = new ParameterBlock();
        params.addSource(im);
        params.add(0.5F);         // x scale factor
        params.add(0.5F);         // y scale factor
        params.add(0.0F);         // x translate
        params.add(0.0F);         // y translate
        params.add(interp);       // interpolation method

        /* Create an operator to scale image1. */
        RenderedOp image2 = JAI.create("scale", params);
        
        pb.addSource(image2);
        float kData[] = new float[9];

        float alpha;
        if (param1 > 150) {
            alpha = (param1-125.0f)/25.0f;
        } else {
            alpha = param1/150.0f;
        }
        float beta = (1.0f-alpha)/8.0f;
        for (int i = 0; i < 9; i++) {
            kData[i] = beta;
        }
        kData[4] = alpha;

        KernelJAI k = new KernelJAI(3,3,1,1,kData);
        pb.add(k);
        return JAI.create("convolve", pb);
    }

    private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider2StateChanged
        // TODO add your handling code here:
        jTextField2.setText(Integer.toString(jSlider2.getValue()));
        JSlider source = (JSlider)evt.getSource();
        int value = source.getValue();
        if (source == jSlider2) {
             param1 = value;
        }
        repaint();   
    }//GEN-LAST:event_jSlider2StateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
           this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Blur().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSlider jSlider2;
    private javax.swing.JTextField jTextField2;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    // End of variables declaration//GEN-END:variables

}
